<template>
  <div class="execution">
    <div>
      <div class="add_but">
        <el-button
          type="primary"
          style="margin-right: 20px"
          v-hasPermi="['book:media:add']"
          @click="handleAddNews"
          size="mini"
        >新 增
        </el-button>
      </div>
      <el-dialog
        :title="operateMaterial == 'add' ? '新建图文' : '修改图文'"
        append-to-body
        :before-close="dialogNewsClose"
        :close-on-click-modal="false"
        :visible.sync="dialogNewsVisible"
        width="80%"
        top="20px"
      >
        <div v-loading="addMaterialLoading">
          <div class="input-tt">标题：</div>
          <el-input
            v-model="articlesAdd.title"
            placeholder="请输入标题"
          ></el-input>
          <div class="input-tt">文章链接：</div>
          <el-input
            v-model="articlesAdd.url"
            placeholder="请输入标题"
          ></el-input>
          <div class="input-tt">封面：</div>
          <div>
            <div class="thumb-div">
              <div class="thumb-but">
                <upload-img
                  v-model="articlesAdd.pic"
                  :operate="operate"
                  :limit="1"
                />
              </div>
            </div>
            <el-dialog
              title="选择图片"
              :visible.sync="dialogImageVisible"
              width="80%"
              append-to-body
            >
              <WxMaterialSelect
                :objData="{ repType: 'image' }"
                @selectMaterial="selectMaterial"
              ></WxMaterialSelect>
            </el-dialog>
          </div>
        </div>
        <div
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogNewsVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="subNews"
          >提 交</el-button>
        </div>
      </el-dialog>
      <div
        class="waterfall"
        v-loading="tableLoading"
      >
        <div
          class="waterfall-item"
          v-for="item in tableData"
          :key="item.id"
        >
          <MediaInfo :objData="item"></MediaInfo>
          <el-row class="ope-row">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleEditNews(item)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delMaterial(item)"
            ></el-button>
          </el-row>
        </div>
      </div>
      <div
        v-if="tableData.length <= 0 && !tableLoading"
        class="el-table__empty-block"
      >
        <span class="el-table__empty-text">暂无数据</span>
      </div>
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page.sync="page.currentPage"
        :page-sizes="[10, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        class="pagination"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getPage, addObj, delObj, putObj, publish } from "@/api/wxmp/wxdraft";
import {
  newsImgUpload,
  materialFileUpload,
  publishArticle,
  listByPage,
  addArticle,
  deleteById,
  edit,
} from "@/api/book/media";
import { getPage as getPage1 } from "@/api/wxmp/wxmaterial";
import WxEditor from "@/components/Editor/WxEditor.vue";
import MediaInfo from "@/components/show/MediaInfo.vue";
import WxMaterialSelect from "@/components/wx-material-select/main.vue";
import { getToken } from "@/utils/auth";

export default {
  name: "wxdraft",
  components: {
    WxEditor,
    MediaInfo,
    WxMaterialSelect,
  },
  data () {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      page1: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      tableLoading: false,
      actionUrl:
        process.env.VUE_APP_BASE_API + "/wxmaterial/materialFileUpload",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileList: [],
      dialogVideoVisible: false,
      dialogNewsVisible: false,
      addMaterialLoading: false,
      uploadData: {
        mediaType: "image",
        title: "image",
        introduction: "image",
      },
      articlesAdd: {
        title: "",
        thumbMediaId: "",
        author: "",
        digest: "",
        showCoverPic: "",
        content: "",
        contentSourceUrl: "",
        needOpenComment: "",
        onlyFansCanComment: "",
        thumbUrl: "",
      },
      isActiveAddNews: 0,
      dialogImageVisible: false,
      imageListData: [],
      tableLoading1: false,
      operateMaterial: "add",
      articlesMediaId: "",
      hackResetEditor: false,
      operate: ''
    };
  },
  watch: {},
  created () { },
  mounted: function () {
    this.getPage(this.page);
  },
  computed: {},
  methods: {
    aaa (val) {
      this.status = val;
      this.page.currentPage = 1; // 当前页数
      this.getPage(this.page);
    },
    getPage (page, params) {
      this.tableData = [];
      this.tableLoading = true;
      listByPage(
        Object.assign(
          {
            pageNum: page.currentPage,
            pageSize: page.pageSize,
          },
          params
        )
      )
        .then((response) => {
          console.log(response);
          this.tableData = response.rows;
          this.page.total = response.total;
          this.page.currentPage = page.currentPage;
          this.page.pageSize = page.pageSize;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    sizeChange (val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
      this.getPage(this.page);
    },
    currentChange (val) {
      this.page.currentPage = val;
      this.getPage(this.page);
    },
    delMaterial (item) {
      this.$confirm("此操作将永久删除该草稿, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.tableLoading = true;
        deleteById(item)
          .then((response) => {
            this.tableLoading = false;
            if (response.code == 200) {
              this.getPage(this.page);
            } else {
              this.tableLoading = false;
              this.$message.error("删除出错：" + response.msg);
            }
          })
          .catch(() => {
            this.tableLoading = false;
          });
      });
    },
    getPage1 () {
      this.tableLoading1 = true;
      listByPage({
        pageNum: this.page1.currentPage,
        pageSize: this.page1.pageSize,
        type: "image",
      })
        .then((response) => {
          this.tableLoading1 = false;
          this.imageListData = response.data.items;
          this.page1.total = response.data.totalCount;
        })
        .catch(() => {
          this.tableLoading1 = false;
        });
    },
    openMaterial () {
      this.imageListData = null;
      this.page1.currentPage = 1;
      this.getPage1();
      this.dialogImageVisible = true;
    },
    dialogNewsClose (done) {
      this.$confirm("修改内容可能还未保存，确定关闭吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.hackResetEditor = false; //销毁组件
          this.$nextTick(() => {
            this.hackResetEditor = true; //重建组件
          });
          this.isActiveAddNews = 0;
          done();
        })
        .catch(() => { });
    },
    downNews (index) {
      let temp = this.articlesAdd[index];
      this.articlesAdd[index] = this.articlesAdd[index + 1];
      this.articlesAdd[index + 1] = temp;
      this.isActiveAddNews = index + 1;
    },
    upNews (index) {
      let temp = this.articlesAdd[index];
      this.articlesAdd[index] = this.articlesAdd[index - 1];
      this.articlesAdd[index - 1] = temp;
      this.isActiveAddNews = index - 1;
    },
    activeNews (index) {
      this.hackResetEditor = false; //销毁组件
      this.$nextTick(() => {
        this.hackResetEditor = true; //重建组件
      });
      this.isActiveAddNews = index;
    },
    minusNews (index) {
      this.$confirm("确定删除该图文吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.articlesAdd.splice(index, 1);
          if (this.isActiveAddNews == index) {
            this.isActiveAddNews = 0;
          }
        })
        .catch(() => { });
    },
    plusNews () {
      this.articlesAdd.push({
        title: "",
        thumbMediaId: "",
        author: "",
        digest: "",
        showCoverPic: "",
        content: "",
        contentSourceUrl: "",
        needOpenComment: "",
        onlyFansCanComment: "",
        thumbUrl: "",
      });
      this.isActiveAddNews = this.articlesAdd.length - 1;
    },
    subNews () {
      this.addMaterialLoading = true;
      if (this.operateMaterial == "add") {
        addArticle({
          id: this.articlesAdd.id,
          pic: this.articlesAdd.pic,
          url: this.articlesAdd.url,
          title: this.articlesAdd.title,
        })
          .then((response) => {
            this.addMaterialLoading = false;
            this.dialogNewsVisible = false;
            if (response.code == 200) {
              this.isActiveAddNews = 0;
              this.articlesAdd = {
                title: "",
                thumbMediaId: "",
                author: "",
                digest: "",
                showCoverPic: "",
                content: "",
                contentSourceUrl: "",
                needOpenComment: "",
                onlyFansCanComment: "",
                thumbUrl: "",
                pic: "",
                url: "",
                title: "",
              };

              this.page.currentPage = 1;
              this.getPage(this.page);
            } else {
              this.$message.error("新增图文出错：" + response.msg);
            }
          })
          .catch(() => {
            this.addMaterialLoading = false;
          });
      }
      if (this.operateMaterial == "edit") {
        edit({
          id: this.articlesAdd.id,
          pic: this.articlesAdd.pic,
          url: this.articlesAdd.url,
          title: this.articlesAdd.title,
        })
          .then((response) => {
            this.addMaterialLoading = false;
            this.dialogNewsVisible = false;
            if (response.code == 200) {
              this.isActiveAddNews = 0;
              this.articlesAdd = {
                title: "",
                thumbMediaId: "",
                author: "",
                digest: "",
                showCoverPic: "",
                content: "",
                contentSourceUrl: "",
                needOpenComment: "",
                onlyFansCanComment: "",
                thumbUrl: "",
                pic: "",
                url: "",
                title: "",
              };

              this.getPage(this.page);
            } else {
              this.$message.error("修改图文出错：" + response.msg);
            }
          })
          .catch(() => {
            this.addMaterialLoading = false;
          });
      }
    },
    handleEditNews (item) {
      this.hackResetEditor = false; //销毁组件
      this.$nextTick(() => {
        this.hackResetEditor = true; //重建组件
      });
      if (this.operateMaterial == "add") {
        this.isActiveAddNews = 0;
      } else {
        this.isActiveAddNews = 0;
      }
      this.operateMaterial = "edit";
      this.articlesAdd = JSON.parse(JSON.stringify(item));
      this.articlesMediaId = item.mediaId;
      this.dialogNewsVisible = true;
    },
    handleAddNews () {
      this.hackResetEditor = false; //销毁组件
      this.$nextTick(() => {
        this.hackResetEditor = true; //重建组件
      });
      if (this.operateMaterial == "edit") {
        this.isActiveAddNews = 0;
        this.articlesAdd = {
          title: "",
          thumbMediaId: "",
          author: "",
          digest: "",
          showCoverPic: "",
          content: "",
          contentSourceUrl: "",
          needOpenComment: "",
          onlyFansCanComment: "",
          thumbUrl: "",
        };
      }
      this.operateMaterial = "add";
      this.dialogNewsVisible = true;
    },
    beforeThumbImageUpload (file) {
      this.addMaterialLoading = true;
      const isType =
        file.type === "image/jpeg" ||
        file.type === "image/png" ||
        file.type === "image/gif" ||
        file.type === "image/bmp" ||
        file.type === "image/jpg";
      const isLt = file.size / 1024 / 1024 < 2;
      if (!isType) {
        this.$message.error("上传图片格式不对!");
      }
      if (!isLt) {
        this.$message.error("上传图片大小不能超过2M!");
      }
      this.addMaterialLoading = false;
      return isType && isLt;
    },
    handleUploadSuccess (response, file, fileList) {
      this.tableLoading = false;
      this.addMaterialLoading = false;
      if (response.code == 200) {
        this.dialogVideoVisible = false;
        this.fileList = [];
        this.uploadData.title = "i";
        this.uploadData.introduction = "i";
        this.articlesAdd.thumbMediaId = response.data.mediaId;
        this.articlesAdd.thumbUrl = response.data.url;
      } else {
        this.$message.error("上传出错：" + response.msg);
      }
    },
    selectMaterial (item) {
      this.dialogImageVisible = false;
      this.articlesAdd.thumbMediaId = item.mediaId;
      this.articlesAdd.thumbUrl = item.url;
    },
    handlePublishNews (item) {
      this.$confirm(
        "你正在通过发布的方式发表内容。 发布不占用群发次数，一天可多次发布。已发布内容不会推送给用户，也不会展示在公众号主页中。 发布后，你可以前往发表记录获取链接，也可以将发布内容添加到自定义菜单、自动回复、话题和页面模板中。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.tableLoading = true;
          publishArticle(item)
            .then((response) => {
              this.tableLoading = false;
              this.$message.success("发布任务提交成功");
              this.getPage(this.page);
            })
            .catch(() => {
              this.tableLoading = false;
            });
        })
        .catch(() => { });
    },
  },
};
</script>

<style lang="scss" scoped>
.pagination {
  float: right;
  margin-right: 25px;
}

.add_but {
  padding: 10px;
}

.ope-row {
  margin-top: 5px;
  text-align: center;
  border-top: 1px solid #eaeaea;
  padding-top: 5px;
}

.item-name {
  font-pagesize: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.el-upload__tip {
  margin-left: 5px;
}

/*新增图文*/
.left {
  display: inline-block;
  width: 35%;
  vertical-align: top;
  margin-top: 200px;
}

.right {
  display: inline-block;
  width: 60%;
  margin-top: -40px;
}

.avatar-uploader {
  width: 20%;
  display: inline-block;
}

.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  text-align: unset !important;
}

.avatar-uploader .el-upload:hover {
  border-color: #165dff;
}

.avatar-uploader-icon {
  border: 1px solid #d9d9d9;
  font-pagesize: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 230px;
  height: 120px;
}

.avatar1 {
  width: 120px;
  height: 120px;
}

.digest {
  width: 60%;
  display: inline-block;
  vertical-align: top;
}

/*新增图文*/
/*瀑布流样式*/
.waterfall {
  width: 100%;
  column-gap: 10px;
  column-count: 5;
  margin: 0 auto;
}

.waterfall-item {
  padding: 10px;
  margin-bottom: 10px;
  break-inside: avoid;
  border: 1px solid #eaeaea;
}

p {
  line-height: 30px;
}

@media (min-width: 992px) and (max-width: 1300px) {
  .waterfall {
    column-count: 3;
  }
  p {
    color: red;
  }
}

@media (min-width: 768px) and (max-width: 991px) {
  .waterfall {
    column-count: 2;
  }
  p {
    color: orange;
  }
}

@media (max-width: 767px) {
  .waterfall {
    column-count: 1;
  }
}

/*瀑布流样式*/
.news-main {
  background-color: #ffffff;
  width: 100%;
  margin: auto;
  height: 120px;
}

.news-content {
  background-color: #acadae;
  width: 100%;
  height: 120px;
  position: relative;
}

.news-content-title {
  display: inline-block;
  font-pagesize: 15px;
  color: #ffffff;
  position: absolute;
  left: 0px;
  bottom: 0px;
  background-color: black;
  width: 98%;
  padding: 1%;
  opacity: 0.65;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  height: 25px;
}

.news-main-item {
  background-color: #ffffff;
  padding: 5px 0px;
  border-top: 1px solid #eaeaea;
  width: 100%;
  margin: auto;
}

.news-content-item {
  position: relative;
  margin-left: -3px;
}

.news-content-item-title {
  display: inline-block;
  font-pagesize: 12px;
  width: 70%;
}

.news-content-item-img {
  display: inline-block;
  width: 25%;
  background-color: #acadae;
}

.input-tt {
  padding: 5px;
}

.activeAddNews {
  border: 5px solid #2bb673;
}

.news-main-plus {
  width: 280px;
  text-align: center;
  margin: auto;
  height: 50px;
}

.icon-plus {
  margin: 10px;
  font-pagesize: 25px;
}

.select-item {
  width: 60%;
  padding: 10px;
  margin: 0 auto 10px auto;
  border: 1px solid #eaeaea;
}

.father .child {
  display: none;
  text-align: center;
  position: relative;
  bottom: 25px;
}

.father:hover .child {
  display: block;
}

.thumb-div {
  display: inline-block;
  width: 30%;
  text-align: center;
}

.thumb-but {
  margin: 5px;
}

.material-img {
  width: 100%;
  height: 100%;
}
</style>
